package com.andaily.domain.dto.user;

import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.shared.security.SecurityUtils;
import com.andaily.domain.user.User;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class UserDto extends AbstractDTO {

    private String nickName;
    private String email;
    private boolean allowEdit;

    public UserDto() {
    }

    public UserDto(User user) {
        this.guid = user.guid();
        this.nickName = user.nickName();
        this.email = user.email();
        this.allowEdit = this.guid.equals(SecurityUtils.currentUserGuid());
    }

    public String getShowName() {
        if (this.allowEdit) {
            return "我";
        }
        if (StringUtils.isNotEmpty(nickName)) {
            return nickName;
        }
        return email;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public static List<UserDto> toDtos(List<User> userList) {
        List<UserDto> dtoList = new ArrayList<UserDto>(userList.size());
        for (User user : userList) {
            dtoList.add(new UserDto(user));
        }
        return dtoList;
    }

    public boolean isAllowEdit() {
        return allowEdit;
    }
}
